package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author violet
 * @since 2022-04-25 10:38:06
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserService userService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info(orders.toString());
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 手机端用户顶端查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> page(int page, int pageSize) {
        //构造分页构造器
        Page<Orders> ordersPage = new Page(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        ordersService.page(ordersPage, queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");
        //取出第一次page对象里面的records(存放着数据) 进行改造
        List<Orders> records = ordersPage.getRecords();

        List<OrdersDto> ordersDtoList = records.stream().map(item -> {
            //将order基本信息拷贝到orderDto里面
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            //构造查询条件 根据订单的id号去OrderDetail去查对应的详情数据(存放在列表里面)
            LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getOrderId, item.getNumber());
            List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper1);
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }

    /**
     * 管理端订单查询(所有用户的下单信息)
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> adminOrderPage(int page, int pageSize) {
        //构造分页构造器
        Page<Orders> ordersPage = new Page(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page(page, pageSize);
        ordersService.page(ordersPage);

        //对象拷贝
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");
        //取出第一次page对象里面的records(存放着数据) 进行改造
        List<Orders> records = ordersPage.getRecords();

        List<OrdersDto> ordersDtoList = records.stream().map(item -> {
            //将order基本信息拷贝到orderDto里面
            OrdersDto ordersDto = new OrdersDto();
            User user = userService.getById(item.getUserId());
            BeanUtils.copyProperties(item, ordersDto);
            //构造查询条件 根据订单的id号去OrderDetail去查对应的详情数据(存放在列表里面)
            LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getOrderId, item.getNumber());
            List<OrderDetail> orderDetailList = orderDetailService.list(queryWrapper1);
            ordersDto.setOrderDetails(orderDetailList);

            //构造查询条件查询用户姓名
            ordersDto.setUserName(user.getName());
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }

    @PutMapping()
    public R<String> chagngeOrderStatus(@RequestBody Orders orders) {
        log.info(orders.toString());
        ordersService.updateById(orders);

        return R.success("");
    }


}

